package com.code.client.proxy;

import com.code.api.RpcRequest;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author lyf
 * @date 2021-10-10 10:51
 */
public class RemoteInvokeHandler implements InvocationHandler {

    private String host;
    private int port;

    public RemoteInvokeHandler(String host, int port) {
        this.host = host;
        this.port = port;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        RpcRequest request = new RpcRequest();
        request.setClassName(method.getDeclaringClass().getName());
        request.setMethodName(method.getName());
        request.setParams(args);

        RpcNetTransport transport = new RpcNetTransport(host, port);
        Object result = transport.sendRequest(request);

        return result;
    }
}
